package com.vortex.cloud.sample.easyexcel.nange.writeexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.vortex.cloud.sample.easyexcel.nange.domain.ExcelData;

import java.util.*;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-08 周五
 * @time 9:06
 * @description
 */
public class WriteExcelTest {
    public static void main(String[] args) {

        List<ExcelData> list = parseData();
        //list 写入 excel 文件中
        EasyExcel.write("G:\\easyexcel\\用户数据表_副本.xls")
                .head(ExcelData.class)
                .excelType(ExcelTypeEnum.XLS)
                .sheet("用户数据表")
                .doWrite(list);

    }


    public static List<ExcelData> parseData() {
        List<ExcelData> list = new LinkedList<>();
        EasyExcel.read("G:\\easyexcel\\用户数据表.xls")
                //把解析的实体类传进来ExcelData 实体类型，默认是 LinkedList
                .head(ExcelData.class)
                .sheet()
                .registerReadListener(new AnalysisEventListener<ExcelData>() {

                    @Override
                    public void invoke(ExcelData excelData, AnalysisContext analysisContext) {
                        list.add(excelData);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        //通知文件读取完毕
                        System.out.println("文件读取完毕!!!");
                    }
                }).doRead();
        return list;
    }
}
